/**create by liuhua at 2020年10月22日 下午4:36:12**/
package com.star.aries.controller;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.star.aries.constant.ErrorCodeEnum;
import com.star.aries.dto.BaseResult;
import com.star.aries.dto.res.CalculationResponseDto;
import com.star.aries.exception.HnServiceException;
import com.star.aries.service.CalculationService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Validated
@Tag(name = "计算")
@RestController
@RequestMapping("/cal")
public class CalController {
  @Autowired
  private CalculationService calculationService;
  
  @PostMapping(value = "/do")
  @Operation(summary = "计算", method = "POST")
  public BaseResult<CalculationResponseDto> getCal(
      @Parameter(description = "车型id") @RequestParam(required = false) @NotNull(message = "trainTypeId 必填") Long trainTypeId,
      @Parameter(description = "位置 1车门") @RequestParam(required = false) Integer position,
      @Parameter(description = "是否重新加载") @RequestParam(required = false) @NotNull(message = "reload 必填") boolean reload
      ) {
    try {
      CalculationResponseDto res = calculationService.getCal(trainTypeId, position, reload);
      return BaseResult.success(res);
    } catch (HnServiceException e) {
      return BaseResult.fail(e.getCode(), e.getMsg());
    } catch (Throwable e) {
      log.error(e.getMessage(), e);
      return BaseResult.fail(ErrorCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }
  }
}
